/**
 * 
 */
package valfazz.android.karfinder.util;

import java.util.List;

import valfazz.android.karfinder.R;
import valfazz.android.karfinder.activity.SongViewerActivity;
import valfazz.android.karfinder.database.DatabaseHelper;
import valfazz.android.karfinder.pojo.Song;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Val
 *
 */
public class SongListView {
	private static final String TAG = "SongListView";
	private Context context;
	private DatabaseHelper dbHelper;
	private ListView listView;
	private SongAdapter songAdapter;
	//private int selected;
	
	/*
	public SongListView(Context context, int listViewResource, String query){
		this.context = context;
		this.songData = new ArrayList<Song>();
		this.listView = (ListView)((Activity)context).findViewById(listViewResource);
		
		openDb();
		
		this.listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				TextView tv = (TextView)view.findViewById(R.id.tvSongIDAndTitle);
				Intent intent = new Intent(getLocalContext(), SongViewerActivity.class);
				intent.putExtra("songId", tv.getText().subSequence(0, 5));
				getLocalContext().startActivity(intent);
			}
			
		});
		
		this.songAdapter = new SongAdapter();
		this.listView.setAdapter(this.songAdapter);
	} */	
	
	public SongListView(Context context, int listViewResource) {
		this.context = context;
		this.listView = (ListView)((Activity)context).findViewById(listViewResource);
		this.dbHelper = new DatabaseHelper(context);
		
		this.listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				TextView tv = (TextView)view.findViewById(R.id.tvSongIDAndTitle);
				Intent intent = new Intent(getLocalContext(), SongViewerActivity.class);
				intent.putExtra("songId", tv.getText().subSequence(0, 5));
				getLocalContext().startActivity(intent);
			}
		});
		
		this.listView.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				final int songId = dbHelper.searchList.get(position).getId();
				
				AlertDialog.Builder contextMenu = new AlertDialog.Builder(getLocalContext());
				contextMenu.setItems(R.array.songlistview_context_menu,
									 new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
							case 0: // add to favorite
								dbHelper.setFavorite(songId + "", true);
								Toast.makeText(getLocalContext(), R.string.added_to_favorite,
											   Toast.LENGTH_SHORT).show();
								break;
/*							case 1: // add to a playlist

								selected = -1;
								final ArrayList<Playlist> playlistList = dbHelper.fetchPlaylistList();
								final String[] playlistNames = new String[playlistList.size()];
								for (int i = 0; i < playlistList.size(); ++i)
									playlistNames[i] = playlistList.get(i).getName();
								AlertDialog.Builder playlistPicker = new AlertDialog.Builder(getLocalContext());
								playlistPicker.setTitle(R.string.choose_a_playlist);
								playlistPicker.setSingleChoiceItems(playlistNames, -1,
										new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog, int which) {
										Log.i("playlist position", playlistNames[which]);
										selected = which;
									}
								});
								playlistPicker.setPositiveButton(R.string.add_to_playlist,
										new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog, int which) {
										Log.i("final position", selected + "");
										if (selected != -1) {
											long resultCode = dbHelper.addSongToPlaylist(playlistList.get(selected).getId(),
																						 songId);
											Toast.makeText(getLocalContext(),
														   ((resultCode != -1) ? R.string.added_to_playlist : R.string.added_to_playlist_already),
														   Toast.LENGTH_SHORT).show();
										}
									}
								});
								playlistPicker.setNegativeButton(R.string.dialog_button_playlist_cancel, null);
								playlistPicker.show();
								break;*/
							default: break;
						}
					}
				});
				contextMenu.show();
				
				return false;
			}
			
		});
		
		songAdapter = new SongAdapter();
		listView.setAdapter(songAdapter);
	}
	
	/**
	 * @author Val
	 */
	protected Context getLocalContext() {
		return this.context;
	}

	public void resetList() {
		dbHelper.searchList.clear();
		songAdapter.notifyDataSetChanged();
	}	
	
	public void setQuery(String query) {
		Log.i("SONG rawQuery from " + TAG, query);
		dbHelper.updateSongList(query);
		songAdapter.notifyDataSetChanged();
		Log.i("songAdapter size", songAdapter.getCount() + "");
	}
	
	public ListView getListView(){
		return listView;
	}
	
	public List<Song> getSongData(){
		return dbHelper.searchList;
	}
	
	public void openDb() {
		this.dbHelper.open(getLocalContext());
	}
	
	public void closeDb() {
		this.dbHelper.close();
	}
	
	/**
	 * @author Val
	 * @version 1
	 * @since 30-10-2011
	 * 
	 */
	private class SongAdapter extends ArrayAdapter<Song> {
		private static final String TAG = "SongAdapter";
		
		public SongAdapter() {
			super(context, R.layout.song_item, dbHelper.searchList);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.i(TAG, "[getView] position " + position);
			SongHolder songHolder;
			
			if (convertView == null) {	
				convertView = LayoutInflater.from(context).inflate(R.layout.song_item, parent, false);	
				songHolder = new SongHolder(convertView);
				convertView.setTag(songHolder);
			} else {
				songHolder = (SongHolder) convertView.getTag();
			}
			
			songHolder.getSongIDAndTitle()
					  .setText(dbHelper.searchList.get(position).getId() + " | " + 
							   dbHelper.searchList.get(position).getTitle());
			songHolder.getSongLyrics()
					  .setText(dbHelper.searchList.get(position).getLyrics());
			
			return convertView;
		}
	}
	
	static class SongHolder {
		private View base;
		private TextView songIDAndTitle;
		private TextView songLyrics;

		public SongHolder(View base) {
			songIDAndTitle = songLyrics = null;
			this.base = base;
		}

		public TextView getSongIDAndTitle() {
			if (this.songIDAndTitle == null){
				this.songIDAndTitle = (TextView)base.findViewById(R.id.tvSongIDAndTitle);
			}
			return songIDAndTitle;
		}

		public TextView getSongLyrics() {
			if (this.songLyrics == null) {
				this.songLyrics = (TextView)base.findViewById(R.id.tvSongLyrics);
			}
			return songLyrics;
		}
	}
}
